<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h2>在线教育平台</h2>
        <div class="tabs">
          <div 
            class="tab" 
            :class="{ active: activeRole === 'student' }" 
            @click="activeRole = 'student'"
          >
            学生登录
          </div>
          <div 
            class="tab" 
            :class="{ active: activeRole === 'teacher' }" 
            @click="activeRole = 'teacher'"
          >
            教师登录
          </div>
        </div>
      </div>
      
      <el-form 
        ref="loginFormRef" 
        :model="loginForm" 
        :rules="loginRules" 
        label-position="top"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名" 
            :prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            :prefix-icon="Lock"
            @keyup.enter="handleLogin" 
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            class="login-button" 
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="demo-accounts">
        <p class="demo-title">演示账号:</p>
        <template v-if="activeRole === 'student'">
          <p>用户名: student1</p>
          <p>密码: 123456</p>
        </template>
        <template v-else>
          <p>用户名: teacher1</p>
          <p>密码: 123456</p>
        </template>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { User, Lock } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { useUserStore } from '../store/user';

// 表单引用
const loginFormRef = ref(null);

// 当前选择的角色: student 或 teacher
const activeRole = ref('student');

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
});

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3到20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
  ]
};

// 登录状态
const loading = ref(false);

// 获取用户状态管理器
const userStore = useUserStore();

// 登录处理
const handleLogin = async () => {
  // 表单验证
  if (!loginFormRef.value) return;
  
  try {
    const valid = await loginFormRef.value.validate();
    if (!valid) return;
  } catch (error) {
    return;
  }
  
  // 执行登录
  loading.value = true;
  try {
    const success = await userStore.login(loginForm, activeRole.value);
    if (success) {
      ElMessage.success('登录成功');
      // 路由跳转在store action中已处理
    } else {
      ElMessage.error('登录失败，请检查用户名和密码');
    }
  } catch (error) {
    console.error('登录发生错误：', error);
    ElMessage.error('登录失败，请稍后再试');
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
}

.login-card {
  width: 380px;
  padding: 30px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 20px;
}

.tabs {
  display: flex;
  border-bottom: 1px solid #EBEEF5;
  margin-bottom: 20px;
}

.tab {
  flex: 1;
  text-align: center;
  padding: 10px 0;
  cursor: pointer;
  color: #606266;
  transition: all 0.3s;
}

.tab.active {
  color: #409EFF;
  border-bottom: 2px solid #409EFF;
}

.login-form {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
}

.demo-accounts {
  font-size: 12px;
  color: #909399;
  background: #f3f5f9;
  padding: 10px;
  border-radius: 4px;
  margin-top: 20px;
}

.demo-title {
  font-weight: bold;
  margin-bottom: 5px;
}

.demo-accounts p {
  margin: 5px 0;
}
</style>
